package cn.itcast.bos.web.action.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;

@ParentPackage("json-default")
@Namespace("/")
@Actions
@Controller
@Scope("prototype")
public class StandardActionn extends ActionSupport implements ModelDriven<Standard> {
	//模型驱动
	private Standard standard=new Standard();
	@Override
	public Standard getModel() {
		// TODO Auto-generated method stub
		return standard;
	}
	
	//注入Serivice
	@Autowired
	private StandardService standSerivice;
	
	
	private int page;
	private int rows;
	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}


	@Action(value="standard_save",results={
			@Result(name="success",location="/pages/base/standard.html",type="redirect")
	})
	//保存数据的方法
	public String save(){
		standSerivice.save(standard);
		return SUCCESS;
	}
	
	
	//分页查询的方法
	@Action(value="standard_pageQuery",results={
			@Result(name="success",type="json")
	})
	public String standard_pageQuery(){
		//调用serive层,
		Pageable pageable=new PageRequest(page-1,rows);
		Page<Standard> page=standSerivice.findAll(pageable);
		//将在查询总条数和记录集合封装进map集合中
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("total",page.getTotalElements());
		map.put("rows",page.getContent());
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
	
	//查询所有快递员的信息
	@Action(value="standard_findAll",results={
			@Result(name="success",type="json")
			})
	public String standard_findAll(){
		List<Standard> standard=standSerivice.findAll();
		ActionContext.getContext().getValueStack().push(standard);
		return SUCCESS;
	}
}
